package com.yxhl.zoume.core.busticket.presenter;

import com.yxhl.protobuf.YxScheduleEntry;
import com.yxhl.protobuf.YxScheduleRequest;
import com.yxhl.protobuf.YxScheduleResponse;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.busticket.view.BusServicesQueryView;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.busticket.GetBusServicesUseCase;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by Xboy on 16/4/8.
 */
public class BusServicesQueryPresenter implements IPresenter {
    private BusServicesQueryView busServicesQueryView;

    private GetBusServicesUseCase getBusServicesUseCase;

    @Inject
    public BusServicesQueryPresenter(GetBusServicesUseCase getBusServicesUseCase) {
        this.getBusServicesUseCase = getBusServicesUseCase;
    }

    public void queryBusServices(YxScheduleRequest param, boolean needShowDialog) {
        busServicesQueryView.waitingForQueryBusServices(needShowDialog);

        getBusServicesUseCase.execute(param, new ZMSubscriber<YxScheduleResponse>() {

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                busServicesQueryView.waitingForQueryBusServices(false);
            }

            @Override
            public void onNext(YxScheduleResponse getBusServicesResponse) {
                super.onNext(getBusServicesResponse);
                busServicesQueryView.waitingForQueryBusServices(false);

                List<YxScheduleEntry> yxBusSchedulesList = getBusServicesResponse.getYxBusSchedulesList();
                busServicesQueryView.renderServices(yxBusSchedulesList);
            }
        });
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        getBusServicesUseCase.unsubscribe();
    }

    @Override
    public void attachView(BaseView view) {
        busServicesQueryView = (BusServicesQueryView) view;
    }
}
